package com.example.logisticsingle.utils;

import com.example.logisticsingle.Config.SSOConfig;
import com.example.logisticsingle.String.StringUtils;
import com.example.logisticsingle.dto.Result;
import com.example.logisticsingle.dto.TokenInfo;

import com.example.logisticsingle.dto.UsernamePasswordLoginParams;
import com.example.logisticsingle.entity.SSOUser;
import com.example.logisticsingle.exception.SSOServerException;

import lombok.extern.slf4j.Slf4j;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;


import java.util.Map;


/**
 * 获取登录信息的

 */
@Slf4j
public class SSOLoginHelper {
    /**
     * 校验Token
     * @param token
     * @return
     */
    public static SSOUser validateToken(String token) throws Exception{
        String ssoServer = SSOConfig.SSO_SERVER;
        if(StringUtils.isNull(ssoServer)) {
            Exception e = new SSOServerException("未设置登录服务器");
            log.error(e.getMessage());
            throw e;
        }
        String validateUrl = String.format("%s/authentication/validate/token/%s", ssoServer,token);
        log.info("访问地址：{}",validateUrl);
        ParameterizedTypeReference<Result<SSOUser>> typeReference = new ParameterizedTypeReference<Result<SSOUser>>() {};
        ResponseEntity<Result<SSOUser>> entity = RestTemplateUtils.exchangeByTypeReference(validateUrl, HttpMethod.GET,
                null, typeReference);
        Result<SSOUser> result = entity.getBody();
//		@SuppressWarnings("rawtypes")
//		ResponseEntity<Result> entity = RestTemplateUtils.get(validateUrl, Result.class);
//		Result<?> result = entity.getBody();
        log.info("response:{}",result);
//		if(null!=result&&result.isStatus()) {
//			SSOUser user = JsonUtils.fromJson(result.getData(), SSOUser.class);
//			return user;
//		}
        if(null!=result&&result.isStatus()) {
            SSOUser user = result.getData();
            return user;
        }
        return null;
    }
    /**
     * 登录
     * @param headers
     * @param params
     * @return
     * @throws Exception
     */
    public static TokenInfo login(Map<String, String> headers, UsernamePasswordLoginParams params) throws Exception {
        String ssoServer = SSOConfig.SSO_SERVER;
        if(StringUtils.isNull(ssoServer)) {
            Exception e = new SSOServerException("未设置登录服务器");
            log.error(e.getMessage());
            throw e;
        }
        String loginUrl = String.format("%s/authentication/form", ssoServer);
        log.info("访问地址：{}",loginUrl);
        ParameterizedTypeReference<Result<TokenInfo>> typeReference = new ParameterizedTypeReference<Result<TokenInfo>>() {};
        ResponseEntity<Result<TokenInfo>> entity = RestTemplateUtils.exchangeByTypeReference(loginUrl, HttpMethod.POST,headers,params, typeReference);
        Result<TokenInfo> result = entity.getBody();
        log.info("response:{}",result);
        if(null!=result&&result.isStatus()) {
            TokenInfo tokenInfo = result.getData();
            return tokenInfo;
        }
        return null;
    }
    /**
     *退出登录
     * @param headers
     * @return
     * @throws Exception
     */
    public static boolean logout(Map<String, String> headers) throws Exception {
        String ssoServer = SSOConfig.SSO_SERVER;
        if(StringUtils.isNull(ssoServer)) {
            Exception e = new SSOServerException("未设置登录服务器");
            log.error(e.getMessage());
            throw e;
        }
        String logoutURL = String.format("%s/authentication/logout", ssoServer);
        log.info("访问地址：{}",logoutURL);
        ParameterizedTypeReference<Result<SSOUser>> typeReference = new ParameterizedTypeReference<Result<SSOUser>>() {};
        ResponseEntity<Result<SSOUser>> entity = RestTemplateUtils.exchangeByTypeReference(logoutURL, HttpMethod.POST,headers,null, typeReference);
        Result<SSOUser> result = entity.getBody();
        log.info("response:{}",result);
        if(null!=result&&result.isStatus()) {
            return true;
        }
        return false;
    }
}
